import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  data () {
    return {
      // 控制进度条的不走
      step: 0,
      // 控制第二个进度
      curSelected: 'msg',
      // 添加商品表单数据
      goodsForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
        goods_cat: [],
        is_promote: false,
        pics: [],
        goods_introduce: ''
      },
      // 联级菜单数据(分类列表数据)
      cateList: [],
      // 上传图片需要携带的请求头
      headers: {
        Authorization: localStorage.getItem('token')
      }

    }
  },
  methods: {
    // 点击任意列表触发同步
    synchronous (ele) {
      // 使用:value需要这一步  使用v-model不需要
      // this.curSelected = ele.name
      this.step = +ele.index
    },
    // 点击下一步
    nextStep (curSelected, step) {
      this.curSelected = curSelected
      this.step = step
    },
    // 文件上传成功后的钩子
    picUploadSuccess (res, file, fileList) {
      if (res.meta.status === 200) {
        this.goodsForm.pics.push({pic: res.data.tmp_path})
      }
      // console.log('r', response, 'file', file, 'la', fileList)
    },
    // 添加商品
    async addGoods () {
      /* eslint-disable camelcase */
      const {goods_name, goods_price, goods_weight, goods_number, is_promote, pics, goods_introduce, goods_cat} = this.goodsForm
      let data = {
        goods_name,
        goods_price,
        goods_weight,
        goods_number,
        is_promote,
        pics,
        goods_introduce,
        goods_cat: goods_cat.join(',')
      }
      const res = await this.axios.post('goods', data)
      console.log(res)
      const { data: { meta: { msg, status } } } = res
      if (status === 201) {
        this.$message({
          type: 'success',
          message: msg
        })
        this.$router.push('/goods')
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
      }
    }
  },
  async created () {
    const res = await this.axios.get('categories', {
      params: {
        type: 3
      }
    })
    if (res.data.meta.status === 200) {
      this.cateList = res.data.data
    }
  },
  // 富文本组件
  components: {
    quillEditor
  }
}
